import {createRouter,createWebHashHistory} from 'vue-router'
import home1 from '../views/Home1.vue';
import Page1 from '../views/Page1.vue';
import Page2 from '../views/Page2.vue';
import Page3 from '../views/Page3.vue';
import Page4 from '../views/Page4.vue';
import Introduce from "../views/Introduce.vue";
import test from "../views/test.vue";
import newView from "../views/newView.vue";
import BackView from "../views/BackView.vue";
import loginView from "../views/LoginView.vue";
import store from "../store/index.js";

const routes = [
    {
        path :'/',
        component:()=>import('../views/Main.vue'),
        children:[
            {
             path:'/',
            name:'home1',
            component: home1
            },
            {
                path: '/page1',
                name: 'page1',
                component: Page1 
              },
              {
                path: '/page2',
                name: 'page2',
                component: Page2 
              },
              {
                path: '/page3',
                name: 'page3',
                component: Page3 
              },
              {
                path: '/page4',
                name: 'page4',
                component: Page4 
              },
        ]
    },{
    path: "/introduce",
        name:"introduce",
        component:Introduce
    },
    {
    path: "/test",
    name:"test",
    component:test
},
    {
        path: "/new",
        name:"new",
        component:newView
    },
    {
        path: "/back",
        name: "back",
        component:BackView
    },{
        path: "/login",
        name: "login",
        component:loginView
    }



]
const router = createRouter(
    {
        history:createWebHashHistory(),
        routes
    }
)


router.beforeEach((to, from, next) => {
    //创建守卫规则集合(这里表示'/home'与'/news'路径是需要权限验证的)
    const nextRoute = ['/main', '/news','/','/back'];
    // 使用isLogin来模拟是否登录
    let isLogin = store.getters.getLoginStates;
    // 判断to.path(要跳转的路径)是否是需要权限验证的
    if (nextRoute.indexOf(to.path) >= 0) {
        if (!isLogin) {
            router.push({
                path: '/login'
            })
            //location.reload();
        }
    }
    next(); //必须要有
});

export default router 